﻿using LdtJudge.DataEngine.Common.XmlUtility;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.Collection;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.OptimzeMode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using LdtJudge.DataEngine.Model.DataModel.SysCommon;
using LdtDataEngin.ModuleDetail.BaseModule.MultiFack;
using System.Diagnostics;
using LdtJudge.DataEngine.Model.XMLConfigModel.SameNodeConfigModel;
using Ldt.InformationUnit.Common;
using LdtJudge.DataEngine.CallInterface.ConfiCenter;
using Newtonsoft.Json;
using LdtJudge.DataEngine.Model.XMLConfigModel.OptimizeConfigModel;
using LdtJudge.DataEngine.Data.DataParseStorage;
using LdtJudge.DataEngine.Model.OptimizationModel;
using System.Xml.Linq;
using LdtJudge.DataEngine.Model.EvidenceDerivation;

namespace LdtDataEngin.ModuleDetail.CommonModule.PersonMerger
{
    /// <summary>
    /// 分人员要素合并处理
    /// </summary>
    public class MoudlePersonMergerDetail : BaseModule.Optimization.OptimizationBase
    {
        ///<summary>
        /// 数据交互对象
        ///</summary> 
        public InteractiveShowClass InteractiveShow { get; }
        ///<summary>
        /// 案由集合
        ///</summary> 
        private List<CaseCauseModel> CaseCausesList { get; }

        /// <summary>
        /// 优选用到的卷宗Code，但是这里可能有问题
        /// </summary>
        private string SourceCode { get; }

        /// <summary>
        /// 实例化构造函数
        /// </summary>
        /// <param name="dataContextIn">上下文数据</param>
        /// <param name="moudleName">模块名称</param>
        public MoudlePersonMergerDetail(DataContext dataContextIn, string moudleName)
        {
            base.dataContext = dataContextIn;
            this.ModuleName = moudleName;
            CaseCausesList = base.dataContext.DataCommon.CaseCausesList;
            InteractiveShow = base.dataContext.InteractiveShow;
            SourceCode = base.dataContext.DataCommon.SourceCode;
        }

        /// <summary>
        /// 加载分人员优选需要的数据
        /// </summary>
        private void LoadData()
        {
            if (dataContext.DataLayerOpt.ElementOptimizationResults == null)
            {//将分析的数据赋值给优选结果
                InteractiveShow.LogShow("----------将分析的数据赋值给优选结果------");
                dataContext.DataLayerOpt.ElementOptimizationResults = dataContext.DataLayerAns.ElementAnalsisDataModelResults;
            }
            else
            {
                InteractiveShow.LogShow("----------本身具有案由优选结果------");
            }
          
        }
        /// <summary>
        /// 要素优选合并
        /// </summary>
        //public void FactorMerge()
        //{
        //    Stopwatch stopwatch = new Stopwatch();
        //    SetModuleProcess(ModuleName, 0,dataContext.InteractiveShow.inputParameterModel);
        //    stopwatch.Start();
        //    //加载要素节点
        //    string strxmlTemplate = this.CaseCausesList?.First().ElementXML;
        //    XmlDocument xmlDocumentTemplate = new XmlDocument();
        //    xmlDocumentTemplate.LoadXml(strxmlTemplate);
        //    XmlNode nodetemplate = xmlDocumentTemplate.SelectSingleNode("//事实要素");
        //    var sameNodeConfig = GetSameNodeConfig();
        //    List<SameNodeXml> xmlConfigModels = sameNodeConfig.xmllable.ToList();
        //    //如果要素优选的配置不为空，则进行数据优选;
        //    InteractiveShow.LogShow("----------开始加 载优选需要的数据-------开始------");
        //    //加载数据源数据 
        //    LoadData();
        //    ElementMergeBase elementMerge = new ElementMergeBase(base.dataContext);
        //    var keySourceData = elementMerge.LoadDataSource();
        //    var optimizeDataMo = elementMerge.GetListElmentModel();
        //    MultiFackSituationPlot multiFackSituationPlot = new MultiFackSituationPlot() { dataContext = dataContext };
        //    multiFackSituationPlot.Situation(optimizeDataMo);

        //    GetEvideQusetionFack(nodetemplate, xmlConfigModels);
        //    InteractiveShow.LogShow("----------开始加载优选需要的数据--------结束-----");

        //    InteractiveShow.LogShow("----------加载证据效力、质证意见数据----------开始-------");

        //    #region 证据效力、质证意见数据加载
        //    XmlDocument RDxmlDocument = null;

        //    Dictionary<Tuple<string, string>, XmlDocument> dicFacke = new Dictionary<Tuple<string, string>, XmlDocument>();
        //    Dictionary<Tuple<string, string>, XmlDocument> dicFgFacke = new Dictionary<Tuple<string, string>, XmlDocument>();

        //    if (dataContext.DataLayerOpt.EviFactElementRelations != null && dataContext.DataLayerOpt.EviFactElementRelations.Count > 0)
        //    {
        //        XmlUtilCommon xmlUtilCommon = new XmlUtilCommon();
        //        MultiFactUnion multiFactunion = new MultiFactUnion() { dataContext = base.dataContext };
        //        var RdModelList = dataContext.DataLayerOpt.EviFactElementRelations.Where(a => a.EvidenceAffirmStatus == "1" || a.EvidenceAffirmStatus == "2").ToList();
        //        var DRDModelList = dataContext.DataLayerOpt.EviFactElementRelations.Where(a => a.EvidenceAffirmStatus == "0").ToList();
               
        //        foreach (var rdmodel in RdModelList)
        //        {
        //            DataModel dataModel = dataContext.DataLayerOpt.ElementOptimizationResults.FirstOrDefault(a => a.ID == rdmodel.FactElementId);
        //            List<DataModel> dataModelList = xmlUtilCommon.SubParentDateModel(dataContext.DataLayerOpt.ElementOptimizationResults, dataModel);
        //            XmlDocument xmlDocument = xmlUtilCommon.DataModelByXml(dataModelList, dataModelList.FirstOrDefault(a => a.XmlPath == "案由要素" + @"/" + "事实要素").ParentId);
        //            string xml = $"<案由要素 案由名称=\"{dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_Name}\" 案由ID=\"{dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_ID}\">" + xmlDocument.OuterXml + "</案由要素>";
        //            xmlDocument.LoadXml(xml);
        //            if (RDxmlDocument == null)
        //            {
        //                RDxmlDocument = xmlDocument;
        //            }
        //            else
        //            {
        //                multiFactunion.MultiFactUnionXml(nodetemplate, xmlDocument.ChildNodes[0].ParentNode, RDxmlDocument.ChildNodes[0].ParentNode, xmlConfigModels, RDxmlDocument.ChildNodes[0].ParentNode, MultiFactBase.SameNodeHandleType.b);
        //            }
        //        }
        //        foreach (var DRDmodel in DRDModelList)
        //        {
        //            DataModel dataModel = dataContext.DataLayerOpt.ElementOptimizationResults.FirstOrDefault(a => a.ID == DRDmodel.FactElementId);
        //            List<DataModel> dataModelList = xmlUtilCommon.SubParentDateModel(dataContext.DataLayerOpt.ElementOptimizationResults, dataModel);
        //            XmlDocument xmlDocument = xmlUtilCommon.DataModelByXml(dataModelList, dataModelList.FirstOrDefault(a => a.XmlPath == "案由要素" + @"/" + "事实要素").ParentId);
        //            string xml = $"<案由要素 案由名称=\"{dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_Name}\" 案由ID=\"{dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_ID}\">" + xmlDocument.OuterXml + "</案由要素>";
        //            xmlDocument.LoadXml(xml);
        //            Tuple<string, string> tuple = new Tuple<string, string>(DRDmodel.ProponentSituid, DRDmodel.ProponentName);
        //            XmlDocument confirmdocumenttemp = new XmlDocument();
        //            confirmdocumenttemp.LoadXml(string.Format("<{0} 案由名称=\"{1}\" 案由ID=\"{2}\"></{0}>", "案由要素",dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_Name, dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_ID));
        //            XmlNode xmlNodeFacttConfirmtemp = confirmdocumenttemp.CreateElement("事实要素");
        //            confirmdocumenttemp.DocumentElement.AppendChild(xmlNodeFacttConfirmtemp);
        //            if (dicFacke.ContainsKey(tuple))
        //            {
        //                multiFactunion.MultiFactUnionXml(nodetemplate, xmlDocument.ChildNodes[0].ParentNode, dicFacke[tuple].ChildNodes[0].ParentNode, xmlConfigModels, xmlNodeFacttConfirmtemp, MultiFactBase.SameNodeHandleType.b);
        //            }
        //            else
        //            {
        //                dicFacke.Add(tuple, xmlDocument);
        //            }
        //        }
        //    }
        //    #endregion

        //    InteractiveShow.LogShow("----------加载证据效力、质证意见数据----------结束-------");

        //    InteractiveShow.LogShow("----------加载法官认定、不认定个要素----------开始-------");
        //    DOCLocationModel locationModel = new DOCLocationModel();
        //    locationModel = base.dataContext.DataLayerSys.locationModelList.FirstOrDefault(a => a.DocType.ToLower() == SourceCode.ToLower());
        //    //获取要素类型6、7 法官认定、不认定要素。
        //    if (!string.IsNullOrEmpty(locationModel.JudgeElementPaht))
        //    {
        //        DataModel dataModel = XmlUtility.ConditionFirstOrDefaultDataModel(dataContext.DataLayerOpt.SourdatModleList, "", locationModel.JudgeElementPaht);
        //        if (dataModel!=null)
        //        {
        //            XmlUtilCommon xmlUtilCommon = new XmlUtilCommon() { dataContext = dataContext };
        //            List<DataModel> dataModelList = xmlUtilCommon.SubordinateDataModelList(dataContext.DataLayerOpt.SourdatModleList, dataModel);
        //            List<DataModel> dataModelRootList = dataModelList.Where(a => a.XmlPath.IndexOf("事实要素") > -1 && a.AttributesList.FirstOrDefault(b => b.Name == "要素类型") != null)?.ToList();
        //            MultiFactUnion multiFactunion = new MultiFactUnion() { dataContext = base.dataContext };
        //            foreach (DataModel datamodelRoot in dataModelRootList)
        //            {
        //                AttributeInfo attributeInfo = datamodelRoot.AttributesList.FirstOrDefault(a => a.Name == "要素类型");
        //                if (attributeInfo != null)
        //                {
        //                    XmlDocument xmlDocument = xmlUtilCommon.DataModelByXml(dataContext.DataLayerOpt.SourdatModleList, datamodelRoot.ParentId);
        //                    string saveValue = attributeInfo.sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
        //                    if (saveValue == "6")
        //                    {
        //                        XmlDocument document = new XmlDocument();
        //                        XmlNode xmlNode = xmlDocument.SelectSingleNode("案由要素/事实要素[@要素类型='6']");
        //                        string xml = $"<案由要素 案件名称=\"{dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_Name}\" 案件ID=\"{dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_ID}\" >" + xmlNode.OuterXml + "</案由要素>";
        //                        document.LoadXml(xml);
        //                        if (RDxmlDocument == null)
        //                        {
        //                            RDxmlDocument = document;
        //                        }
        //                        else
        //                        {
        //                            multiFactunion.MultiFactUnionXml(nodetemplate, document.ChildNodes[0].ParentNode, RDxmlDocument.ChildNodes[0].ParentNode, xmlConfigModels, RDxmlDocument.ChildNodes[0].ParentNode, MultiFactBase.SameNodeHandleType.b);
        //                        }

        //                    }
        //                    else if (saveValue == "7")
        //                    {
        //                        XmlNode xmlNode = xmlDocument.SelectSingleNode("案由要素/事实要素[@要素类型='7']");
        //                        XmlNode xmlNodePeron = xmlDocument.SelectSingleNode("案由要素/事实要素[@要素类型='7']/主张方");
        //                        if (xmlNodePeron.Attributes["主张方姓名"] != null && xmlNodePeron.Attributes["主张诉讼地位"] != null)
        //                        {
        //                            string name = xmlNodePeron.Attributes["主张方姓名"].Value;
        //                            string suitid = xmlNodePeron.Attributes["主张诉讼地位"].Value;
        //                            Tuple<string, string> tuple = new Tuple<string, string>(suitid, name);
        //                            XmlDocument document = new XmlDocument();
        //                            string xml = $"<案由要素 案件名称=\"{dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_Name}\" 案件ID=\"{dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_ID}\" >" + xmlNode.OuterXml + "</案由要素>";
        //                            document.LoadXml(xml);
        //                            if (dicFgFacke.ContainsKey(tuple))
        //                            {
        //                                XmlDocument confirmdocumenttemp = new XmlDocument();
        //                                confirmdocumenttemp.LoadXml(string.Format("<{0} 案由名称=\"{1}\" 案由ID=\"{2}\"></{0}>", "案由要素", dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_Name, dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_ID));
        //                                XmlNode xmlNodeFacttConfirmtemp = confirmdocumenttemp.CreateElement("事实要素");
        //                                confirmdocumenttemp.DocumentElement.AppendChild(xmlNodeFacttConfirmtemp);

        //                                multiFactunion.MultiFactUnionXml(nodetemplate, document.ChildNodes[0].ParentNode, dicFgFacke[tuple].ChildNodes[0].ParentNode, xmlConfigModels, xmlNodeFacttConfirmtemp, MultiFactBase.SameNodeHandleType.b);
        //                                dicFgFacke[tuple] = confirmdocumenttemp;
        //                            }
        //                            else
        //                            {
        //                                dicFgFacke.Add(tuple, xmlDocument);
        //                            }
        //                        }
        //                    }
        //                }
        //            }
        //        }
               
        //    }

        //    InteractiveShow.LogShow("----------加载法官认定、不认定个要素----------结束-------");
        //    if (keySourceData.Count > 0)
        //    {
        //        //分人员合并后的数据集合
        //        Dictionary<Tuple<string, string>, XmlNode> personxmlNode = elementMerge.GetFactTypePersonxml(optimizeDataMo, 2);
        //        //获取无争议事实要素内容
        //        Dictionary<Tuple<string, string>, XmlNode> personNoArgumentxmlNode = elementMerge.GetFactTypePersonxml(optimizeDataMo, 4);

        //        MultiFactUnion multiFactunion = new MultiFactUnion() { dataContext = base.dataContext };

        //        //分人员合并后的内容 且对分人员内容进行去重
        //        for (int i = 0; i < personxmlNode.Count; i++)
        //        {
        //            var person = personxmlNode.ToList()[i];
        //            InteractiveShow.LogShow("\r\n----------" + person.Key.Item1 + "-" + person.Key.Item2 + "合并后的内容为：----------\r\n" + person.Value.OuterXml);
        //            XmlDocument confirmdocumenttemp = new XmlDocument();
        //            confirmdocumenttemp.LoadXml(string.Format("<{0} 案由名称=\"{1}\" 案由ID=\"{2}\"></{0}>", nodetemplate.ParentNode.Name,dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_Name,dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_ID));
        //            XmlNode xmlNodeFacttConfirmtemp = confirmdocumenttemp.CreateElement("事实要素");
        //            confirmdocumenttemp.DocumentElement.AppendChild(xmlNodeFacttConfirmtemp);
        //            //加入对事实要素的属性内容进行特殊处理
        //            multiFactunion.MultiFactUnionNode(nodetemplate, person.Value, xmlConfigModels, xmlNodeFacttConfirmtemp, MultiFactBase.SameNodeHandleType.a);
        //            //节点去重合并后进行重新赋值
        //            personxmlNode[person.Key] = confirmdocumenttemp;

        //            InteractiveShow.LogShow("\r\n----------" + person.Key.Item1 + "-" + person.Key.Item2 + "当前节点去重后的内容为：----------\r\n" + xmlNodeFacttConfirmtemp.OuterXml);
        //        }

        //        //step1-0 分人员证据数据 进行证据合并   @加入对事实要素属性内容的特殊处理
        //        EvidElementMerge evidElement = new EvidElementMerge(dataContext = base.dataContext);
        //        XmlNode confirmEvid = evidElement.EvidMerge(nodetemplate, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);
        //        //step1-1 分人员形成最终认定的要素 
        //        MultiFactIntersect multiFactinter = new MultiFactIntersect() { dataContext = base.dataContext };
        //        string confirmxml = multiFactinter.MultiFactHandleCommon(nodetemplate, personxmlNode, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);
        //        if (RDxmlDocument!=null)
        //        {
        //            multiFactunion.MultiFactUnionXml(nodetemplate, multiFactinter.confirmNode, RDxmlDocument.ChildNodes[0], xmlConfigModels, multiFactinter.confirmNode, MultiFactBase.SameNodeHandleType.b);
        //        }
        //        //step1-2 形成最终认定的要素=分人员最终认定+证据最终认定
        //        XmlDocument confirmdocument = new XmlDocument();
        //        confirmdocument.LoadXml(string.Format("<{0} 案由名称=\"{1}\" 案由ID=\"{2}\"></{0}>", nodetemplate.ParentNode.Name,dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_Name,dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_ID));
        //        XmlNode xmlNodeFacttConfirm = confirmdocument.CreateElement("事实要素");
        //        confirmdocument.DocumentElement.AppendChild(xmlNodeFacttConfirm);
        //        //认定证据和分人员认定合并
        //        multiFactunion.MultiFactUnionXml(nodetemplate, multiFactinter.confirmNode, confirmEvid, xmlConfigModels, xmlNodeFacttConfirm, MultiFactBase.SameNodeHandleType.b);

        //        InteractiveShow.LogShow("\r\n----------★step1证据认定与分人员最终认定进行合并后的内容为：★----------\r\n" + confirmdocument.OuterXml);

        //        InteractiveShow.LogShow("\r\n----------★step2最终有异议的要素内容为：★----------\r\n");
        //        //step2-0 分人员形成有异议的要素内容 
        //        MultiFactBase multiFactexc = new MultiFactDifference() { dataContext = base.dataContext };
        //        multiFactexc.MultiFactHandleCommon(nodetemplate, personxmlNode, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);


        //        //step2-1  形成最终有异议 ，进行将认定的要素从有异议中的排除；
        //        MultiFactExcept multiFact = new MultiFactExcept() { dataContext = base.dataContext };
        //        Dictionary<Tuple<string, string>, XmlNode> differentListNode = multiFact.MultiFactExceptXml(nodetemplate, (multiFactexc as MultiFactDifference).DifferenceList, xmlNodeFacttConfirm, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);

        //        //step2-2 将无争议要素（庭审总结）排除有异议，得到最终无争议要素 20200431 ljx 添加庭审总结信息
        //        Dictionary<Tuple<string, string>, XmlNode> finalListNode = multiFact.MultiFactExceptXml(nodetemplate, personNoArgumentxmlNode, differentListNode, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);

        //        InteractiveShow.LogShow("\r\n----------★step2-2最终无争议要素内容为：★----------\r\n");
        //        foreach (var fin in finalListNode)
        //        {
        //            InteractiveShow.LogShow("\r\n" + fin.Value.OuterXml);
        //        }

        //        XmlDocument noargumentdocument = new XmlDocument();
        //        noargumentdocument.LoadXml(string.Format("<{0} 案由名称=\"{1}\" 案由ID=\"{2}\"></{0}>", nodetemplate.ParentNode.Name,dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_Name,dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_ID));
        //        XmlNode xmlNodeFacttNoArgument = noargumentdocument.CreateElement("事实要素");
        //        noargumentdocument.DocumentElement.AppendChild(xmlNodeFacttNoArgument);
        //        foreach (var noArgumentModel in finalListNode)
        //        {
        //            xmlNodeFacttNoArgument.RemoveAll();
        //            multiFactunion.MultiFactUnionXml(nodetemplate, confirmdocument, noArgumentModel.Value, xmlConfigModels, xmlNodeFacttNoArgument, MultiFactBase.SameNodeHandleType.b);
        //            confirmdocument = (XmlDocument)noargumentdocument.Clone();
        //        }
        //        InteractiveShow.LogShow("\r\n----------★step2-2 将上述合并后的认定内容与无争议内容进行合并后的内容为：★----------\r\n" + noargumentdocument.OuterXml);


        //        //step3-0 形成证据最终待认定=证据待认定-最终认定-最终有异议；
        //        Dictionary<Tuple<string, string>, XmlNode> unevidone = multiFact.MultiFactExceptXml(nodetemplate, evidElement.GetEvidPersonxml("0"), xmlNodeFacttConfirm, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);
        //        Dictionary<Tuple<string, string>, XmlNode> unevidtwo = multiFact.MultiFactExceptXml(nodetemplate, unevidone, (multiFactexc as MultiFactDifference).DifferenceList, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);

        //        //step3-1 形成分人员最终待认定=分人员待认定-最终认定-最终有异议；
        //        MultiFactUnConfirm factUnConfirm = new MultiFactUnConfirm() { dataContext = base.dataContext };
        //        factUnConfirm.ConfirmNode = confirmdocument;
        //        factUnConfirm.Personxml = (multiFactexc as MultiFactDifference).DifferenceList;
        //        factUnConfirm.MultiFactHandleCommon(nodetemplate, personxmlNode, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);

        //        //step3-2 形成最终待认定的事实要素
        //        Dictionary<Tuple<string, string>, XmlNode> finalUnMerge = multiFactunion.MultiFactUnionPersonXml(nodetemplate, factUnConfirm.Unpersonxml, unevidtwo, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);
        //        if (dicFacke!=null && dicFacke.Count>0)
        //        {
        //            foreach (var elementFacke in dicFacke)
        //            {
        //                if (finalUnMerge.ContainsKey(elementFacke.Key))
        //                {
        //                    XmlDocument confirmdocumenttemp = new XmlDocument();
        //                    confirmdocumenttemp.LoadXml(string.Format("<{0} 案由名称=\"{1}\" 案由ID=\"{2}\"></{0}>","案由要素", dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_Name, dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_ID));
        //                    XmlNode xmlNodeFacttConfirmtemp = confirmdocumenttemp.CreateElement("事实要素");
        //                    confirmdocumenttemp.DocumentElement.AppendChild(xmlNodeFacttConfirmtemp);
        //                    multiFactunion.MultiFactUnionXml(nodetemplate, finalUnMerge[elementFacke.Key], elementFacke.Value, xmlConfigModels, xmlNodeFacttConfirmtemp, MultiFactBase.SameNodeHandleType.b);
        //                    finalUnMerge[elementFacke.Key] = confirmdocumenttemp;
        //                }
        //                else
        //                {
        //                    finalUnMerge.Add(elementFacke.Key,elementFacke.Value);
        //                }
        //            }
        //        }

        //        foreach (var per in finalUnMerge)
        //        {
        //            InteractiveShow.LogShow("\r\n----------★step3分人员最终待认定的要素为 " + per.Key.Item2 + "(" + per.Key.Item1 + ")：★----------\r\n" + per.Value.OuterXml);
        //        }
        //        //step4 合并 最终认定+最终待认定
        //        XmlNode mernode = multiFactunion.MultiFactUnionPersonXml(nodetemplate, finalUnMerge, factUnConfirm.ConfirmNode, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);
                
        //        ////step5 全部数据合并
        //        ////获取到分人员诉请要素
        //        Dictionary<Tuple<string, string>, XmlNode> suitPersonxmlNode = elementMerge.GetFactTypePersonxml(optimizeDataMo, 1);
        //        Dictionary<Tuple<string, string>, XmlNode> resultxmlNode = elementMerge.GetFactTypePersonxml(optimizeDataMo,3);
        //        XmlDocument nodeMerge = multiFactunion.MultFactAllMerge(nodetemplate.ParentNode.Name, suitPersonxmlNode,resultxmlNode, confirmdocument, differentListNode, finalUnMerge, mernode);
        //        multiFackSituationPlot.UpdateMerge(multiFackSituationPlot.crimeSituationPlotDataModels, multiFackSituationPlot.relevantSituationPlotDataModels, nodeMerge);

        //        //step6 产生核心要素的缺失要素
        //        MultiFactKernel factkernel = new MultiFactKernel();
        //        string kernelfact = factkernel.MutiBuildKernelFact(nodetemplate, nodeMerge);

        //        XmlUtilCommon xmlUtilCommon = new XmlUtilCommon() { dataContext = base.dataContext };
        //        DataSoursModel dataSoursModel = new DataSoursModel();
                
        //        dataSoursModel = xmlUtilCommon.GetXMLByDataModel(nodeMerge);
        //        if (dataSoursModel!=null  && dataSoursModel.dataModelList.Count>0)
        //        {
        //            foreach (var mode in dataSoursModel.dataModelList)
        //            {
        //                mode.XmlPath = mode.XmlPath.Replace("案由要素/案由要素", "案由要素");
        //            }
        //        }
        //        xmlUtilCommon.DataModelIDByReset(dataSoursModel.dataModelList, "", "");
        //        XmlDocument txml = xmlUtilCommon.DataModelByXml(dataSoursModel.dataModelList, "");
        //        base.dataContext.DataLayerOpt.MergeElementXml = txml;
        //        base.dataContext.DataLayerOpt.MergeElementDataList = JsonConvert.DeserializeObject<List<DataModel>>(JsonConvert.SerializeObject(dataSoursModel.dataModelList));
        //        locationModel = base.dataContext.DataLayerSys.locationModelList.FirstOrDefault(a => a.DocType.ToLower() == SourceCode.ToLower());
        //        xmlUtilCommon.CaseNodeUpdate(base.dataContext.DataLayerOpt.SourdatModleList, locationModel.MergeElmentPath);
        //        string prentID = XmlUtility.ConditionFirstOrDefaultDataModel(dataContext.DataLayerOpt.SourdatModleList, "", locationModel.MergeElmentPath).ID;
        //        try
        //        {
        //            DataModel dataModelRoot = XmlUtility.ConditionFirstOrDefaultDataModel(base.dataContext.DataLayerOpt.SourdatModleList,"", locationModel.MergeElmentPath);
        //            if (dataModelRoot!=null)
        //            {
        //                List<DataModel> dataModelRovmeList = xmlUtilCommon.SubordinateDataModelList(base.dataContext.DataLayerOpt.SourdatModleList, dataModelRoot);
        //                if (dataModelRovmeList.Count > 0)
        //                {
        //                    foreach (var item in base.dataContext.DataLayerOpt.SourdatModleList.Where(a => a.ParentId == prentID))
        //                    {
        //                        base.dataContext.DataLayerOpt.SourdatModleList.Remove(item);
                            
        //                    }
        //                }
        //            }0
        //        }
        //        catch (Exception elemtex)
        //        {
        //            InteractiveShow.LogShow("\r\n----------★清除数据错误★----------\r\n" + elemtex.ToString(), LogMessageType.Error);
        //        }
        //        dataSoursModel.dataModelList.FirstOrDefault(a => a.ParentId == "").ParentId = prentID;
        //        base.dataContext.DataLayerOpt.SourdatModleList.AddRange(dataSoursModel.dataModelList);
        //        XmlDocument xmlDocument = XmlUtility.DataModelByXml(base.dataContext.DataLayerOpt.SourdatModleList);
        //        base.dataContext.DataLayerOpt.OptimizationXml = xmlDocument;
        //        InteractiveShow.LogShow("\r\n----------★最终合并后的内容结构为：★----------\r\n" + nodeMerge.OuterXml);
        //        InteractiveShow.LogShow("\r\n----------★最终合并推导后的内容结构为：★----------\r\n" + nodeMerge.OuterXml);
        //        InteractiveShow.LogShow("\r\n----------★最终合并后的内容结构为：★----------\r\n" + xmlDocument.OuterXml);

                
        //    }
        //    stopwatch.Stop();
        //    SetModuleProcess(ModuleName + "_" + stopwatch.Elapsed.TotalMilliseconds.ToString("0"), 100,dataContext.InteractiveShow.inputParameterModel);
        //}

        /// <summary>
        /// 要素合并功能
        /// </summary>
        public void FactorMerge() 
        {
            Stopwatch stopwatch = new Stopwatch();
            SetModuleProcess(ModuleName, 0, dataContext.InteractiveShow.inputParameterModel);
            stopwatch.Start();

            DOCLocationModel locationModel = new DOCLocationModel();
            locationModel = base.dataContext.DataLayerSys.locationModelList.FirstOrDefault(a => a.DocType.ToLower() == SourceCode.ToLower());
            XmlUtilCommon xmlUtilCommon = new XmlUtilCommon() { dataContext = base.dataContext };
           
            //加载数据
            LoadData();
            base.dataContext.DataLayerOpt.MergeElementXml = new Dictionary<string, XmlDocument>();
            base.dataContext.DataLayerOpt.MergeElementDataList = new Dictionary<string, List<DataModel>>();
            foreach (var Resultdatamodel in dataContext.DataLayerOpt.ElementOptimizationResults)
            {
                string casecauseid = Resultdatamodel.Key;
                CaseCauseModel caseCauseModel = CaseCausesList.FirstOrDefault(a => a.CC_ID == casecauseid);
                string strxmlTemplate = this.CaseCausesList.FirstOrDefault(a=> a.CC_ID==casecauseid).ElementXML;
                XmlDocument xmlDocumentTemplate = new XmlDocument();
                xmlDocumentTemplate.LoadXml(strxmlTemplate);
                XmlNode nodetemplate = xmlDocumentTemplate.SelectSingleNode("//事实要素");
                var sameNodeConfig = GetSameNodeConfig();
                List<SameNodeXml> xmlConfigModels = sameNodeConfig.xmllable.ToList();
                var judgeConfirmSameNodeConfig = GetJudgeConfirmSameNodeConfig();

                List<SameNodeXml> judgeSameConfigModels = new List<SameNodeXml>();
                if (judgeConfirmSameNodeConfig.xmllable != null)
                {
                    judgeSameConfigModels = judgeConfirmSameNodeConfig.xmllable.ToList();
                }
                

                //如果要素优选的配置不为空，则进行数据优选;
                InteractiveShow.LogShow("----------开始加 载优选需要的数据-------开始------");

                #region 优选数据加载

                ElementMergeBase elementMerge = new ElementMergeBase(base.dataContext);
                var keySourceData = elementMerge.LoadDataSource(Resultdatamodel.Value, casecauseid);
                var optimizeDataMo = elementMerge.GetListElmentModel(Resultdatamodel.Value, casecauseid);
                MultiFackSituationPlot multiFackSituationPlot = new MultiFackSituationPlot() { dataContext = dataContext };
                multiFackSituationPlot.Situation(optimizeDataMo,casecauseid);

                #endregion

                InteractiveShow.LogShow("----------开始加载优选需要的数据--------结束-----");

                InteractiveShow.LogShow("----------加载证据效力、质证意见数据----------开始-------");

                #region 证据效力、质证意见数据加载
                XmlDocument RDxmlDocument = null; 
                Dictionary<Tuple<string, string>, XmlDocument> dicFacke = new Dictionary<Tuple<string, string>, XmlDocument>();
                Dictionary<Tuple<string, string>, XmlDocument> dicFgFacke = new Dictionary<Tuple<string, string>, XmlDocument>();
                EvidElementMerge evidElement = new EvidElementMerge(dataContext = base.dataContext);

                if (dataContext.DataLayerOpt.EviFactElementRelations != null && dataContext.DataLayerOpt.EviFactElementRelations.Count > 0)
                {
                    MultiFactUnion multiFactunion = new MultiFactUnion() { dataContext = base.dataContext };
                    var RdModelList = dataContext.DataLayerOpt.EviFactElementRelations.Where(a => a.EvidenceAffirmStatus == "1" || a.EvidenceAffirmStatus == "2").ToList();
                    var DRDModelList = dataContext.DataLayerOpt.EviFactElementRelations.Where(a => a.EvidenceAffirmStatus == "0").ToList();

                    foreach (var rdmodel in RdModelList)
                    {
                        DataModel dataModel = Resultdatamodel.Value.FirstOrDefault(a => a.ID == rdmodel.FactElementId);
                        List<DataModel> dataModelList = xmlUtilCommon.SubParentDateModel(Resultdatamodel.Value, dataModel);
                        XmlDocument xmlDocument = xmlUtilCommon.DataModelByXml(dataModelList, dataModelList.FirstOrDefault(a => a.XmlPath == "案由要素" + @"/" + "事实要素").ParentId);
                        string xml = $"<案由要素 案由名称=\"{caseCauseModel.CC_Name}\" 案由ID=\"{casecauseid}\">" + xmlDocument.OuterXml + "</案由要素>";
                        xmlDocument.LoadXml(xml);
                        if (RDxmlDocument == null)
                        {
                            RDxmlDocument = xmlDocument;
                        }
                        else
                        {
                            multiFactunion.MultiFactUnionXml(nodetemplate, xmlDocument.ChildNodes[0].ParentNode, RDxmlDocument.ChildNodes[0].ParentNode, xmlConfigModels, RDxmlDocument.ChildNodes[0].ParentNode, MultiFactBase.SameNodeHandleType.b);
                        }
                    }
                    foreach (var DRDmodel in DRDModelList)
                    {
                        try
                        {
                            DataModel dataModel = Resultdatamodel.Value.FirstOrDefault(a => a.ID == DRDmodel.FactElementId);
                            List<DataModel> dataModelList = xmlUtilCommon.SubParentDateModel(Resultdatamodel.Value, dataModel);
                            XmlDocument xmlDocument = xmlUtilCommon.DataModelByXml(dataModelList, dataModelList.FirstOrDefault(a => a.XmlPath == "案由要素" + @"/" + "事实要素").ParentId);
                            string xml = $"<案由要素 案由名称=\"{caseCauseModel.CC_Name}\" 案由ID=\"{casecauseid}\">" + xmlDocument.OuterXml + "</案由要素>";
                            xmlDocument.LoadXml(xml);
                            Tuple<string, string> tuple = new Tuple<string, string>(DRDmodel.ProponentSituid, DRDmodel.ProponentName);
                            XmlDocument confirmdocumenttemp = new XmlDocument();
                            confirmdocumenttemp.LoadXml(string.Format("<{0} 案由名称=\"{1}\" 案由ID=\"{2}\"></{0}>", "案由要素", caseCauseModel.CC_Name, casecauseid));
                            XmlNode xmlNodeFacttConfirmtemp = confirmdocumenttemp.CreateElement("事实要素");
                            confirmdocumenttemp.DocumentElement.AppendChild(xmlNodeFacttConfirmtemp);
                            if (dicFacke.ContainsKey(tuple))
                            {
                                multiFactunion.MultiFactUnionXml(nodetemplate, xmlDocument.ChildNodes[0].ParentNode, dicFacke[tuple].ChildNodes[0].ParentNode, xmlConfigModels, xmlNodeFacttConfirmtemp, MultiFactBase.SameNodeHandleType.b);
                            }
                            else
                            {
                                dicFacke.Add(tuple, xmlDocument);
                            }
                        }
                        catch (Exception ff)
                        {

                            string tt = "";
                        }
                        
                    }

                    XmlNode confirmRdEvid = evidElement.EvidMerge(nodetemplate, xmlConfigModels, MultiFactBase.SameNodeHandleType.b, "3");
                    if (confirmRdEvid!=null)
                    {
                        if (RDxmlDocument == null)
                        {
                            string xml = $"<案由要素 案由名称=\"{caseCauseModel.CC_Name}\" 案由ID=\"{casecauseid}\"></案由要素>";
                            RDxmlDocument = new XmlDocument();
                            RDxmlDocument.LoadXml(xml);
                            RDxmlDocument.ChildNodes[0].AppendChild(confirmRdEvid);
                        }
                        else
                        {
                            multiFactunion.MultiFactUnionXml(nodetemplate, confirmRdEvid, RDxmlDocument.ChildNodes[0].ParentNode, xmlConfigModels, RDxmlDocument.ChildNodes[0].ParentNode, MultiFactBase.SameNodeHandleType.b);
                        }
                    }
                    var  FGrdList= evidElement.GetEvidPersonxml("4");
                    foreach (var model in FGrdList)
                    {
                        if (dicFacke.ContainsKey(model.Key))
                        {
                            multiFactunion.MultiFactUnionXml(nodetemplate, dicFacke[model.Key].ChildNodes[0].ParentNode, model.Value, xmlConfigModels, dicFacke[model.Key].ChildNodes[0].ParentNode, MultiFactBase.SameNodeHandleType.b);
                        }
                        else
                        {
                            XmlDocument document = new XmlDocument();
                            document.LoadXml(model.Value.OuterXml);
                            dicFacke.Add(model.Key, document);
                        }
                    }
                    
                }
                #endregion

                InteractiveShow.LogShow("----------加载证据效力、质证意见数据----------结束-------");

                InteractiveShow.LogShow("----------加载法官认定、不认定个要素----------开始-------");

                #region 法官认定、不认定数据加载
                locationModel = base.dataContext.DataLayerSys.locationModelList.FirstOrDefault(a => a.DocType.ToLower() == SourceCode.ToLower());
                //获取要素类型6、7 法官认定、不认定要素。
                if (!string.IsNullOrEmpty(locationModel.JudgeElementPaht))
                {
                    string judgeConfirmPath = locationModel.JudgeElementPaht;
                    if (locationModel.JudgeElementPaht.IndexOf("{causecode}") > -1)
                    {
                        //if (dataContext.DataLayerAns.CaseType.ToLower()== "ldzc")
                        //{
                            judgeConfirmPath = locationModel.JudgeElementPaht.Replace("[@案由ID='{causecode}']", "");
                        //}
                       
                    }
                    DataModel dataModel = XmlUtility.ConditionFirstOrDefaultDataModel(dataContext.DataLayerOpt.SourdatModleList, "", judgeConfirmPath);
                    if (dataModel != null)
                    {
                        List<DataModel> dataModelList = xmlUtilCommon.SubordinateDataModelList(dataContext.DataLayerOpt.SourdatModleList, dataModel);
                        List<DataModel> dataModelRootList = dataModelList.Where(a => a.XmlPath.IndexOf("事实要素") > -1 && a.AttributesList.FirstOrDefault(b => b.Name == "要素类型") != null)?.ToList();
                        MultiFactUnion multiFactunion = new MultiFactUnion() { dataContext = base.dataContext };
                        foreach (DataModel datamodelRoot in dataModelRootList)
                        {
                            AttributeInfo attributeInfo = datamodelRoot.AttributesList.FirstOrDefault(a => a.Name == "要素类型");
                            if (attributeInfo != null)
                            {
                                XmlDocument xmlDocument = xmlUtilCommon.DataModelByXml(dataContext.DataLayerOpt.SourdatModleList, datamodelRoot.ParentId);
                                string saveValue = attributeInfo.sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                                if (saveValue == "7")
                                {
                                    XmlDocument document = new XmlDocument();
                                    XmlNode xmlNode = xmlDocument.SelectSingleNode("事实要素[@要素类型='7']");
                                    string xml = $"<案由要素 案件名称=\"{caseCauseModel.CC_Name}\" 案件ID=\"{casecauseid}\" >" + xmlNode.OuterXml + "</案由要素>";
                                    document.LoadXml(xml);
                                    if (RDxmlDocument == null)
                                    {
                                        RDxmlDocument = document;
                                    }
                                    else
                                    {
                                        multiFactunion.MultiFactUnionXml(nodetemplate, document.ChildNodes[0].ParentNode, RDxmlDocument.ChildNodes[0].ParentNode, xmlConfigModels, RDxmlDocument.ChildNodes[0].ParentNode, MultiFactBase.SameNodeHandleType.b);
                                    }

                                }
                                else if (saveValue == "6")
                                {
                                    XmlNode xmlNode = xmlDocument.SelectSingleNode("案由要素/事实要素[@要素类型='6']");
                                    XmlNode xmlNodePeron = xmlDocument.SelectSingleNode("案由要素/事实要素[@要素类型='6']/主张方");
                                    if (xmlNodePeron.Attributes["主张方姓名"] != null && xmlNodePeron.Attributes["主张诉讼地位"] != null)
                                    {
                                        string name = xmlNodePeron.Attributes["主张方姓名"].Value;
                                        string suitid = xmlNodePeron.Attributes["主张诉讼地位"].Value;
                                        Tuple<string, string> tuple = new Tuple<string, string>(suitid, name);
                                        XmlDocument document = new XmlDocument();
                                        string xml = $"<案由要素 案件名称=\"{caseCauseModel.CC_Name}\" 案件ID=\"{casecauseid}\" >" + xmlNode.OuterXml + "</案由要素>";
                                        document.LoadXml(xml);
                                        if (dicFgFacke.ContainsKey(tuple))
                                        {
                                            XmlDocument confirmdocumenttemp = new XmlDocument();
                                            confirmdocumenttemp.LoadXml(string.Format("<{0} 案由名称=\"{1}\" 案由ID=\"{2}\"></{0}>", "案由要素", caseCauseModel.CC_Name, casecauseid));
                                            XmlNode xmlNodeFacttConfirmtemp = confirmdocumenttemp.CreateElement("事实要素");
                                            confirmdocumenttemp.DocumentElement.AppendChild(xmlNodeFacttConfirmtemp);

                                            multiFactunion.MultiFactUnionXml(nodetemplate, document.ChildNodes[0].ParentNode, dicFgFacke[tuple].ChildNodes[0].ParentNode, xmlConfigModels, xmlNodeFacttConfirmtemp, MultiFactBase.SameNodeHandleType.b);
                                            dicFgFacke[tuple] = confirmdocumenttemp;
                                        }
                                        else
                                        {
                                            dicFgFacke.Add(tuple, xmlDocument);
                                        }
                                    }
                                }
                            }
                        }
                    }

                }
                #endregion

                InteractiveShow.LogShow("----------加载法官认定、不认定个要素----------结束-------");
                if (keySourceData.Count > 0)
                {
                    MultiFactUnion multiFactunion = new MultiFactUnion() { dataContext = base.dataContext };
                    #region 优选结果合并
                    //分人员合并后的数据集合
                    Dictionary<Tuple<string, string>, XmlNode> personxmlNode = elementMerge.GetFactTypePersonxml(optimizeDataMo, 2);
                    #region TODO:ZSW 证据与事实要素关系建立
                    //分人员要素节点中添加对应的证据Id
                    if (dataContext.DataLayerOpt.EvidenceFactRelations != null && dataContext.DataLayerOpt.EvidenceFactRelations.Any())
                    {
                        var personRel = dataContext.DataLayerOpt.EvidenceFactRelations.Where(x => x.ElementType == "1").ToList();
                        if (personRel != null && personRel.Any())
                        {
                            for (int i = 0; i < personxmlNode.Count; i++)
                            {
                                var person = personxmlNode.ToList()[i];
                                var xmlContent = person.Value.OuterXml;
                                var newContent = multiFactunion.LoadSourceNodeIdAttr(xmlContent, personRel);
                                XmlDocument xmlTemp = new XmlDocument();
                                xmlTemp.LoadXml(newContent);
                                personxmlNode[person.Key] = (XmlNode)xmlTemp;
                            }
                        }
                    }
                    #endregion
                    //获取无争议事实要素内容
                    Dictionary<Tuple<string, string>, XmlNode> personNoArgumentxmlNode = elementMerge.GetFactTypePersonxml(optimizeDataMo, 4);

                    //分人员合并后的内容 且对分人员内容进行去重
                    for (int i = 0; i < personxmlNode.Count; i++)
                    {
                        var person = personxmlNode.ToList()[i];
                        InteractiveShow.LogShow("\r\n----------" + person.Key.Item1 + "-" + person.Key.Item2 + "合并后的内容为：----------\r\n" + person.Value.OuterXml);
                        XmlDocument confirmdocumenttemp = new XmlDocument();
                        confirmdocumenttemp.LoadXml(string.Format("<{0} 案由名称=\"{1}\" 案由ID=\"{2}\"></{0}>", nodetemplate.ParentNode.Name, caseCauseModel.CC_Name, caseCauseModel.CC_ID));
                        XmlNode xmlNodeFacttConfirmtemp = confirmdocumenttemp.CreateElement("事实要素");
                        confirmdocumenttemp.DocumentElement.AppendChild(xmlNodeFacttConfirmtemp);
                        //加入对事实要素的属性内容进行特殊处理
                        multiFactunion.MultiFactUnionNode(nodetemplate, person.Value, xmlConfigModels, xmlNodeFacttConfirmtemp, MultiFactBase.SameNodeHandleType.a);
                        //节点去重合并后进行重新赋值
                        personxmlNode[person.Key] = confirmdocumenttemp;

                        InteractiveShow.LogShow("\r\n----------" + person.Key.Item1 + "-" + person.Key.Item2 + "当前节点去重后的内容为：----------\r\n" + xmlNodeFacttConfirmtemp.OuterXml);
                    }

                    //step1-0 分人员证据数据 进行证据合并   @加入对事实要素属性内容的特殊处理
                    XmlNode confirmEvid = evidElement.EvidMerge(nodetemplate, xmlConfigModels, MultiFactBase.SameNodeHandleType.b,"1");
                    //step1-1 分人员形成最终认定的要素 
                    MultiFactIntersect multiFactinter = new MultiFactIntersect() { dataContext = base.dataContext };
                    string confirmxml = multiFactinter.MultiFactHandleCommon(nodetemplate, personxmlNode, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);
                    if (RDxmlDocument != null)
                    {
                        XmlDocument confir = new XmlDocument();
                        confir.LoadXml(string.Format("<{0} 案由名称=\"{1}\" 案由ID=\"{2}\"></{0}>", nodetemplate.ParentNode.Name, caseCauseModel.CC_Name, caseCauseModel.CC_ID));
                        XmlNode xmlNodeFac = confir.CreateElement("事实要素");
                        confir.DocumentElement.AppendChild(xmlNodeFac);


                        multiFactunion.MultiFactUnionXml(nodetemplate, multiFactinter.confirmNode, RDxmlDocument, xmlConfigModels, xmlNodeFac, MultiFactBase.SameNodeHandleType.b);
                        multiFactinter.confirmNode = confir;
                    }
                    //step1-2 形成最终认定的要素=分人员最终认定+证据最终认定
                    XmlDocument confirmdocument = new XmlDocument();
                    confirmdocument.LoadXml(string.Format("<{0} 案由名称=\"{1}\" 案由ID=\"{2}\"></{0}>", nodetemplate.ParentNode.Name, caseCauseModel.CC_Name, caseCauseModel.CC_ID));
                    XmlNode xmlNodeFacttConfirm = confirmdocument.CreateElement("事实要素");
                    confirmdocument.DocumentElement.AppendChild(xmlNodeFacttConfirm);
                    //认定证据和分人员认定合并
                    multiFactunion.MultiFactUnionXml(nodetemplate, multiFactinter.confirmNode, confirmEvid, xmlConfigModels, xmlNodeFacttConfirm, MultiFactBase.SameNodeHandleType.b);

                    InteractiveShow.LogShow("\r\n----------★step1证据认定与分人员最终认定进行合并后的内容为：★----------\r\n" + confirmdocument.OuterXml);

                    InteractiveShow.LogShow("\r\n----------★step2最终有异议的要素内容为：★----------\r\n");
                    //step2-0 分人员形成有异议的要素内容 
                    MultiFactBase multiFactexc = new MultiFactDifference() { dataContext = base.dataContext };
                    multiFactexc.MultiFactHandleCommon(nodetemplate, personxmlNode, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);


                    //step2-1  形成最终有异议 ，进行将认定的要素从有异议中的排除；
                    MultiFactExcept multiFact = new MultiFactExcept() { dataContext = base.dataContext };
                    Dictionary<Tuple<string, string>, XmlNode> differentListNode = multiFact.MultiFactExceptXml(nodetemplate, (multiFactexc as MultiFactDifference).DifferenceList, xmlNodeFacttConfirm, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);

                    //step2-2 将无争议要素（庭审总结）排除有异议，得到最终无争议要素 20200431 ljx 添加庭审总结信息
                    Dictionary<Tuple<string, string>, XmlNode> finalListNode = multiFact.MultiFactExceptXml(nodetemplate, personNoArgumentxmlNode, differentListNode, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);

                    InteractiveShow.LogShow("\r\n----------★step2-2最终无争议要素内容为：★----------\r\n");
                    foreach (var fin in finalListNode)
                    {
                        InteractiveShow.LogShow("\r\n" + fin.Value.OuterXml);
                    }

                    XmlDocument noargumentdocument = new XmlDocument();
                    noargumentdocument.LoadXml(string.Format("<{0} 案由名称=\"{1}\" 案由ID=\"{2}\"></{0}>", nodetemplate.ParentNode.Name, caseCauseModel.CC_Name, caseCauseModel.CC_ID));
                    XmlNode xmlNodeFacttNoArgument = noargumentdocument.CreateElement("事实要素");
                    noargumentdocument.DocumentElement.AppendChild(xmlNodeFacttNoArgument);
                    foreach (var noArgumentModel in finalListNode)
                    {
                        xmlNodeFacttNoArgument.RemoveAll();
                        multiFactunion.MultiFactUnionXml(nodetemplate, confirmdocument, noArgumentModel.Value, xmlConfigModels, xmlNodeFacttNoArgument, MultiFactBase.SameNodeHandleType.b);
                        confirmdocument = (XmlDocument)noargumentdocument.Clone();
                    }
                    InteractiveShow.LogShow("\r\n----------★step2-2 将上述合并后的认定内容与无争议内容进行合并后的内容为：★----------\r\n" + noargumentdocument.OuterXml);
                    //step3-0 形成证据最终待认定=证据待认定-最终认定-最终有异议；
                    Dictionary<Tuple<string, string>, XmlNode> unevidone = multiFact.MultiFactExceptXml(nodetemplate, evidElement.GetEvidPersonxml("2"), xmlNodeFacttConfirm, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);
                    Dictionary<Tuple<string, string>, XmlNode> unevidtwo = multiFact.MultiFactExceptXml(nodetemplate, unevidone, (multiFactexc as MultiFactDifference).DifferenceList, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);

                    //step3-1 形成分人员最终待认定=分人员待认定-最终认定-最终有异议；
                    MultiFactUnConfirm factUnConfirm = new MultiFactUnConfirm() { dataContext = base.dataContext };
                    factUnConfirm.ConfirmNode = confirmdocument;
                    factUnConfirm.Personxml = (multiFactexc as MultiFactDifference).DifferenceList;
                    factUnConfirm.MultiFactHandleCommon(nodetemplate, personxmlNode, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);

                    //step3-2 形成最终待认定的事实要素
                    Dictionary<Tuple<string, string>, XmlNode> finalUnMerge = multiFactunion.MultiFactUnionPersonXml(nodetemplate, factUnConfirm.Unpersonxml, unevidtwo, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);
                    //将待认定证据要素合并到最终待认定事实要素当中
                    finalUnMerge= multiFactunion.MultiFactUnionPersonXml(nodetemplate, finalUnMerge, unevidone, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);
                    if (dicFacke != null && dicFacke.Count > 0)
                    {
                        foreach (var elementFacke in dicFacke)
                        {
                            if (finalUnMerge.ContainsKey(elementFacke.Key))
                            {
                                XmlDocument confirmdocumenttemp = new XmlDocument();
                                confirmdocumenttemp.LoadXml(string.Format("<{0} 案由名称=\"{1}\" 案由ID=\"{2}\"></{0}>", "案由要素", caseCauseModel.CC_Name, caseCauseModel.CC_ID));
                                XmlNode xmlNodeFacttConfirmtemp = confirmdocumenttemp.CreateElement("事实要素");
                                confirmdocumenttemp.DocumentElement.AppendChild(xmlNodeFacttConfirmtemp);
                                multiFactunion.MultiFactUnionXml(nodetemplate, finalUnMerge[elementFacke.Key], elementFacke.Value, xmlConfigModels, xmlNodeFacttConfirmtemp, MultiFactBase.SameNodeHandleType.b);
                                finalUnMerge[elementFacke.Key] = confirmdocumenttemp;
                            }
                            else
                            {
                                finalUnMerge.Add(elementFacke.Key, elementFacke.Value);
                            }
                        }
                    }
                    foreach (var per in finalUnMerge)
                    {
                        InteractiveShow.LogShow("\r\n----------★step3分人员最终待认定的要素为 " + per.Key.Item2 + "(" + per.Key.Item1 + ")：★----------\r\n" + per.Value.OuterXml);
                    }
                    //step4 合并 最终认定+最终待认定
                    XmlNode mernode = multiFactunion.MultiFactUnionPersonXml(nodetemplate, finalUnMerge, factUnConfirm.ConfirmNode, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);
                    #region TODO:ZSW 证据与事实要素关系建立
                    //整理最终认定+最终待认定要素与证据节点关系，并处理节点中initialnodeid属性
                    string newMernodeContent = SetEvidMergedElementRelation(mernode.OuterXml);
                    XmlDocument newMernodeDoc = new XmlDocument();
                    newMernodeDoc.LoadXml(newMernodeContent);
                    mernode = (XmlNode)newMernodeDoc;
                    #endregion
                    //step5 合并 最终认定+最终待认定+法官确认回填事实要素
                    XmlDocument judgeConfirmDoc = new XmlDocument();
                    DataSoursModel judgeConfirmSours = GetJudgeConfirmSoursModel();
                    XmlNode xmlNodeJudgeConfirmFacttConfirmtemp = null;
                    if (judgeConfirmSours != null && judgeConfirmSours.dataModelList != null && judgeConfirmSours.dataModelList.Any())
                    {
                        judgeConfirmDoc=XmlUtility.DataModelByXml(judgeConfirmSours.dataModelList);
                        if (!string.IsNullOrWhiteSpace(judgeConfirmDoc.OuterXml))
                        {
                            XmlDocument docGenerateUsageXml = new XmlDocument();
                            docGenerateUsageXml.LoadXml(mernode.OuterXml);
                            XmlDocument judgeConfirmMertemp = new XmlDocument();
                            judgeConfirmMertemp.LoadXml(string.Format("<{0} 案由名称=\"{1}\" 案由ID=\"{2}\"></{0}>", "案由要素", caseCauseModel.CC_Name, caseCauseModel.CC_ID));
                            xmlNodeJudgeConfirmFacttConfirmtemp = judgeConfirmMertemp.CreateElement("事实要素");
                            multiFactunion.MultiFactUnionXml(nodetemplate, docGenerateUsageXml, judgeConfirmDoc, judgeSameConfigModels, xmlNodeJudgeConfirmFacttConfirmtemp, MultiFactBase.SameNodeHandleType.b);
                        }
                    }
                    ////step6 全部数据合并
                    ////获取到分人员诉请要素
                    Dictionary<Tuple<string, string>, XmlNode> suitPersonxmlNode = elementMerge.GetFactTypePersonxml(optimizeDataMo, 1);
                    Dictionary<Tuple<string, string>, XmlNode> resultxmlNode = elementMerge.GetFactTypePersonxml(optimizeDataMo, 3);
                    //排除最终认定要素中的分人员有异议要素
                    confirmdocument = multiFact.MultiFactExceptXml(nodetemplate, confirmdocument, differentListNode, xmlConfigModels, MultiFactBase.SameNodeHandleType.b);
                    XmlDocument nodeMerge = multiFactunion.MultFactAllMerge(nodetemplate.ParentNode.Name, suitPersonxmlNode, resultxmlNode, confirmdocument, differentListNode, finalUnMerge, mernode, xmlNodeJudgeConfirmFacttConfirmtemp, caseCauseModel);
                    multiFackSituationPlot.UpdateMerge(multiFackSituationPlot.crimeSituationPlotDataModels, multiFackSituationPlot.relevantSituationPlotDataModels, nodeMerge);

                    //step7 产生核心要素的缺失要素
                    MultiFactKernel factkernel = new MultiFactKernel();
                    string kernelfact = factkernel.MutiBuildKernelFact(nodetemplate, nodeMerge);
                    DataSoursModel dataSoursModel = new DataSoursModel();
                    dataSoursModel = xmlUtilCommon.GetXMLByDataModel(nodeMerge);
                    if (dataSoursModel != null && dataSoursModel.dataModelList.Count > 0)
                    {
                        foreach (var mode in dataSoursModel.dataModelList)
                        {
                            mode.XmlPath = mode.XmlPath.Replace("案由要素/案由要素", "案由要素");
                        }
                    }
                    xmlUtilCommon.DataModelIDByResetAndRecord(dataSoursModel.dataModelList, "", "","0",null,out List<FactElementNodeCorredRecord> recoreds);
                    #region TODO:ZSW 证据与事实要素关系建立
                    //调整证据与合并后要素关系的事实要素ID
                    if (dataContext.DataLayerOpt.EvidenceFactRelations != null
                    && dataContext.DataLayerOpt.EvidenceFactRelations.Any(x => x.ElementType == "2"))
                    {
                        if (recoreds != null && recoreds.Any())
                        {
                            foreach (var item in dataContext.DataLayerOpt.EvidenceFactRelations.Where(x => x.ElementType == "2"))
                            {
                                var mergeRecord = recoreds.FirstOrDefault(x => x.MergeBeforeId == item.FactElenemtId);
                                if (mergeRecord != null)
                                {
                                    item.FactElenemtId = mergeRecord.MergeAfterId;
                                }
                            }
                        }
                    }
                    #endregion
                    try
                    {
                        XmlDocument txml = xmlUtilCommon.DataModelByXml(dataSoursModel.dataModelList, "");
                        base.dataContext.DataLayerOpt.MergeElementXml.Add(casecauseid, txml);
                        base.dataContext.DataLayerOpt.MergeElementDataList.Add(casecauseid, JsonConvert.DeserializeObject<List<DataModel>>(JsonConvert.SerializeObject(dataSoursModel.dataModelList)));
                        locationModel = base.dataContext.DataLayerSys.locationModelList.FirstOrDefault(a => a.DocType.ToLower() == SourceCode.ToLower());
                        string path = locationModel.MergeElmentPath;
                        if (locationModel.MergeElmentPath.IndexOf("{causecode}") > -1)
                        {
                            if (dataContext.DataLayerAns.CaseType.ToLower() == "ldzc")
                            {
                                path = locationModel.MergeElmentPath.Replace("[@案由ID='{causecode}']", "[@案由ID!='' ]");
                            }
                            else
                            {
                                if (dataContext.DataLayerAns.ExtractCaseCausesList != null && dataContext.DataLayerAns.ExtractCaseCausesList.Count > 0)
                                {
                                    path = locationModel.MergeElmentPath.Replace("{causecode}", dataContext.DataLayerAns.ExtractCaseCausesList.FirstOrDefault(a => a.AllParentId.Contains(casecauseid)).CC_ID);
                                }
                                else
                                {
                                    path = locationModel.MergeElmentPath.Replace("{causecode}", casecauseid);

                                }
                            }


                        }
                        else
                        {
                            path = locationModel.MergeElmentPath.Replace(@"违法行为涉及领域/合并后要素", @"违法行为涉及领域[@领域ID='" + casecauseid + "']/合并后要素").Replace(@"案由信息/详情", @"案由信息/详情[@案由ID='" + casecauseid + "']");
                        }
                        //删除上次合并后要素结果，里面移除了根据xpath添加节点的方法；
                        RemveMergElement(path, casecauseid);
                        xmlUtilCommon.CaseNodeUpdate(base.dataContext.DataLayerOpt.SourdatModleList, path);
                        string prentID = XmlUtility.ConditionFirstOrDefaultDataModel(dataContext.DataLayerOpt.SourdatModleList, "", path)?.ID;
                        dataSoursModel.dataModelList.FirstOrDefault(a => a.ParentId == "").ParentId = prentID;
                        base.dataContext.DataLayerOpt.SourdatModleList.AddRange(dataSoursModel.dataModelList);
                        //加载法官确认要素节点===》
                        //if (judgeConfirmSours != null && judgeConfirmSours.dataModelList?.Count > 0)
                        //{
                        //    string judgeConfirmPath = locationModel.JudgeElementPaht;
                        //    if (locationModel.JudgeElementPaht.IndexOf("{causecode}") > -1)
                        //    {
                        //        //judgeConfirmPath = locationModel.JudgeElementPaht.Replace("{causecode}", casecauseid);
                        //        judgeConfirmPath = locationModel.JudgeElementPaht.Replace("[@案由ID='{causecode}']", "");
                        //    }
                        //    string judageConfirmParentID = XmlUtility.ConditionFirstOrDefaultDataModel(dataContext.DataLayerOpt.SourdatModleList, "", judgeConfirmPath).ID;
                        //    judgeConfirmSours.dataModelList.FirstOrDefault(a => a.ParentId == "").ParentId = judageConfirmParentID;
                        //    base.dataContext.DataLayerOpt.SourdatModleList.AddRange(judgeConfirmSours.dataModelList);
                        //}
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }

                    XmlDocument xmlDocument = XmlUtility.DataModelByXml(base.dataContext.DataLayerOpt.SourdatModleList);
                    base.dataContext.DataLayerOpt.OptimizationXml = xmlDocument;
                    InteractiveShow.LogShow("\r\n----------★最终合并后的内容结构为：★----------\r\n" + nodeMerge.OuterXml);
                    InteractiveShow.LogShow("\r\n----------★最终合并推导后的内容结构为：★----------\r\n" + nodeMerge.OuterXml);
                    InteractiveShow.LogShow("\r\n----------★最终合并后的内容结构为：★----------\r\n" + xmlDocument.OuterXml);

                    #endregion
                }
            }
            stopwatch.Stop();
            SetModuleProcess(ModuleName + "_" + stopwatch.Elapsed.TotalMilliseconds.ToString("0"), 100, dataContext.InteractiveShow.inputParameterModel);
        }
        /// <summary>
        /// 删除已经存在的要素信息
        /// </summary>
        /// <param name="locationModel">要素定位信息</param>
        public void RemveMergElement(string path,string casecauseid) 
        {
            XmlUtilCommon xmlUtilCommon = new XmlUtilCommon() { dataContext = base.dataContext };
            ////locationModel = base.dataContext.DataLayerSys.locationModelList.FirstOrDefault(a => a.DocType.ToLower() == SourceCode.ToLower());
            //这里先注释掉，后续可能有问题；
            //xmlUtilCommon.CaseNodeUpdate(base.dataContext.DataLayerOpt.SourdatModleList, locationModel.MergeElmentPath);
            ////string path = "";
            ////if (locationModel.MergeElmentPath.IndexOf("{causecode}") > -1)
            ////{
            ////    path = locationModel.MergeElmentPath.Replace("{causecode}", casecauseid);
            ////}
            ////else
            ////{
            ////    path = locationModel.MergeElmentPath.Replace(@"违法行为涉及领域/合并后要素", @"违法行为涉及领域[@领域ID='" + casecauseid + "']/合并后要素").Replace(@"案由信息/详情", @"案由信息/详情[@案由ID='" + casecauseid + "']");
            ////}
            DataModel model = XmlUtility.ConditionFirstOrDefaultDataModel(dataContext.DataLayerOpt.SourdatModleList, "", path);
            if (model!=null)
            {
                try
                {
                    List<DataModel> dataModelRoots = XmlUtility.ConditionDataModelList(base.dataContext.DataLayerOpt.SourdatModleList, "", path);
                    if(dataModelRoots!=null && dataModelRoots.Any())
                    {
                        foreach (var rootItem in dataModelRoots)
                        {
                            List<DataModel> dataModelRovmeList = xmlUtilCommon.SubordinateDataModelList(base.dataContext.DataLayerOpt.SourdatModleList, rootItem);
                            if (dataModelRovmeList!=null && dataModelRovmeList.Any())
                            {
                                List<string> datamodelRemoveStr = dataModelRovmeList.Select(a => a.ID).ToList();
                                for (int i = 0; i < datamodelRemoveStr.Count; i++)
                                {
                                    base.dataContext.DataLayerOpt.SourdatModleList.Remove(base.dataContext.DataLayerOpt.SourdatModleList.First(a => a.ID == datamodelRemoveStr[i]));
                                }
                            }
                        }
                    }
                }
                catch (Exception elemtex)
                {
                    InteractiveShow.LogShow("\r\n----------★清除数据错误★----------\r\n" + elemtex.ToString(), LogMessageType.Error);
                }
            }
            
        }

        /// <summary>
        /// 获取同节点判定
        /// </summary>
        /// <returns></returns>
        private SameNodeConfig GetSameNodeConfig()
        {
            var configModel = base.dataContext.DataLayerSys.GetConfigModel(this.ModuleName);
            SameNodeConfig sameNodeConfig = new SameNodeConfig();
            if (base.dataContext.DataLayerAns.CaseCausesList.Count > 0)
            {
                if (!string.IsNullOrWhiteSpace(configModel.ConfigContent))
                {
                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.LoadXml(configModel.ConfigContent);
                    XmlNode xmlNode = xmlDocument.SelectSingleNode("/root/step[@code='samenodejudge']");
                    if (xmlNode != null)
                    {
                        Dictionary<string, string> dicElement = new Dictionary<string, string>();
                        string url = xmlNode.Attributes["executefile"].Value;
                        url = url.Replace("{casetype}", base.dataContext.DataLayerAns.CaseType).Replace("{causecode}", base.dataContext.DataLayerAns.CaseCausesList[0].CC_ID);
                        dicElement.Add("fileFullName", url);
                        string elementMessage = WebUtil.DoGet(ConfigRetrieval.GetConfigRetrieval.GetAppSettingString("systemapi") + "/api/WordParse/GetConfigFileContent", dicElement, "utf-8");
                        dynamic elementcofig = JsonConvert.DeserializeObject<dynamic>(elementMessage);
                        if (elementcofig.result.code.ToString() == "1")
                        {
                            string elementcofigstr = elementcofig.body;
                            sameNodeConfig = XmlUtility.DeserializeToObject<SameNodeConfig>(elementcofigstr);
                            OptimizeSameNodeConfig optimizeSameNodeConfig= XmlUtility.DeserializeToObject<OptimizeSameNodeConfig>(elementcofigstr);
                            if (dataContext.DataLayerSys.optimizeFactConfig==null)
                            {
                                dataContext.DataLayerSys.optimizeFactConfig = new OptimizeFactConfig();
                            }
                            dataContext.DataLayerSys.optimizeFactConfig.OptimizeSameNodeConfig = optimizeSameNodeConfig;
                        }
                    }
                }
                else
                {
                    var config = configModel.ConfigDetail.FirstOrDefault(a => a.Code.ToLower() == "samenodejudge");
                    Dictionary<string, string> dicElement = new Dictionary<string, string>();
                    string url = config.FilePath;
                    url = url.Replace("{casetype}", base.dataContext.DataLayerAns.CaseType).Replace("{causecode}", base.dataContext.DataLayerAns.CaseCausesList[0].CC_ID);
                    dicElement.Add("fileFullName", url);
                    string elementMessage = WebUtil.DoGet(ConfigRetrieval.GetConfigRetrieval.GetAppSettingString("systemapi") + "/api/WordParse/GetConfigFileContent", dicElement, "utf-8");
                    dynamic elementcofig = JsonConvert.DeserializeObject<dynamic>(elementMessage);
                    if (elementcofig.result.code.ToString() == "1")
                    {
                        string elementcofigstr = elementcofig.body;
                        sameNodeConfig = XmlUtility.DeserializeToObject<SameNodeConfig>(elementcofigstr);
                        OptimizeSameNodeConfig optimizeSameNodeConfig = XmlUtility.DeserializeToObject<OptimizeSameNodeConfig>(elementcofigstr);
                        if (dataContext.DataLayerSys.optimizeFactConfig == null)
                        {
                            dataContext.DataLayerSys.optimizeFactConfig = new OptimizeFactConfig();
                        }
                        dataContext.DataLayerSys.optimizeFactConfig.OptimizeSameNodeConfig = optimizeSameNodeConfig;
                    }
                }
            }
            return sameNodeConfig;
        }

        /// <summary>
        /// 获取法官确认同节点判定
        /// </summary>
        private SameNodeConfig GetJudgeConfirmSameNodeConfig()
        {
            var configModel = base.dataContext.DataLayerSys.GetConfigModel(this.ModuleName);
            SameNodeConfig sameNodeConfig = new SameNodeConfig();
            if (base.dataContext.DataLayerAns.CaseCausesList.Count > 0)
            {
                if (!string.IsNullOrWhiteSpace(configModel.ConfigContent))
                {
                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.LoadXml(configModel.ConfigContent);
                    XmlNode xmlNode = xmlDocument.SelectSingleNode("/root/step[@code='samenodejudgeconfirm']");
                    if (xmlNode != null)
                    {
                        Dictionary<string, string> dicElement = new Dictionary<string, string>();
                        string url = xmlNode.Attributes["executefile"].Value;
                        url = url.Replace("{casetype}", base.dataContext.DataLayerAns.CaseType).Replace("{causecode}", base.dataContext.DataLayerAns.CaseCausesList[0].CC_ID);
                        dicElement.Add("fileFullName", url);
                        string elementMessage = WebUtil.DoGet(ConfigRetrieval.GetConfigRetrieval.GetAppSettingString("systemapi") + "/api/WordParse/GetConfigFileContent", dicElement, "utf-8");
                        dynamic elementcofig = JsonConvert.DeserializeObject<dynamic>(elementMessage);
                        if (elementcofig.result.code.ToString() == "1")
                        {
                            string elementcofigstr = elementcofig.body;
                            sameNodeConfig = XmlUtility.DeserializeToObject<SameNodeConfig>(elementcofigstr);
                        }
                    }
                }
                else
                {
                    var config = configModel.ConfigDetail.FirstOrDefault(a => a.Code.ToLower() == "samenodejudgeconfirm");
                    Dictionary<string, string> dicElement = new Dictionary<string, string>();
                    string url = config.FilePath;
                    url = url.Replace("{casetype}", base.dataContext.DataLayerAns.CaseType).Replace("{causecode}", base.dataContext.DataLayerAns.CaseCausesList[0].CC_ID);
                    dicElement.Add("fileFullName", url);
                    string elementMessage = WebUtil.DoGet(ConfigRetrieval.GetConfigRetrieval.GetAppSettingString("systemapi") + "/api/WordParse/GetConfigFileContent", dicElement, "utf-8");
                    dynamic elementcofig = JsonConvert.DeserializeObject<dynamic>(elementMessage);
                    if (elementcofig.result.code.ToString() == "1")
                    {
                        string elementcofigstr = elementcofig.body;
                        sameNodeConfig = XmlUtility.DeserializeToObject<SameNodeConfig>(elementcofigstr);
                    }
                }
            }
            return sameNodeConfig;
        }

        /// <summary>
        /// 获取法官确认数据对象
        /// </summary>
        private DataSoursModel GetJudgeConfirmSoursModel()
        {
            DataSoursModel dataSoursModel = new DataSoursModel();
            if(base.dataContext.DataLayerAns.JudgeConfirmDataModel!=null && base.dataContext.DataLayerAns.JudgeConfirmDataModel.dataModelList?.Count>0)
            {
                dataSoursModel = base.dataContext.DataLayerAns.JudgeConfirmDataModel;
            }
            else
            {
                DAL_An_jiegoushujucunchu dAL_An_Jiegoushujucunchu = new DAL_An_jiegoushujucunchu();
                var jiegoushujucunchu = dAL_An_Jiegoushujucunchu.GetjiegoushujucunchuByType(dataContext.DataLayerAns.Shujuneirong.Shujuid, "5");
                if(jiegoushujucunchu!=null && !string.IsNullOrWhiteSpace(jiegoushujucunchu.Wenjianid))
                dataSoursModel = GetDataSoursModel(ReadDataXML(jiegoushujucunchu.Wenjianid));
            }
            return dataSoursModel;
        }


        /// <summary>
        /// 通过合并后要素xml整理证据与合并后要素关系数据，并去除节点中的initialnodeid属性
        /// </summary>
        private string SetEvidMergedElementRelation(string xmlContent)
        {
            string resultContent = xmlContent;
            if (!string.IsNullOrWhiteSpace(xmlContent))
            {
                XDocument mergedDoc = XDocument.Parse(xmlContent);
                List<string> evidNodeIds = base.dataContext.DataLayerOpt.EvidenceFactRelations
                                                .Where(x => x.ElementType == "1")
                                                .Select(x => x.EvidenceElementId).ToList();
                var evidNodes = mergedDoc.Descendants().Where(x => x.Attribute("initialnodeid") != null && !string.IsNullOrWhiteSpace(x.Attribute("initialnodeid").Value));
                if (evidNodes != null && evidNodes.Any())
                {
                    foreach (XElement nodeItem in evidNodes)
                    {
                        var idAttrValue = nodeItem.Attribute("id")?.Value;
                        if (!string.IsNullOrWhiteSpace(idAttrValue))
                        {
                            var initialnodeids = nodeItem.Attribute("initialnodeid")?.Value;
                            if (!string.IsNullOrWhiteSpace(initialnodeids))
                            {
                                var evidElementIds = initialnodeids.Split(";").ToList();
                                if (evidElementIds != null && evidElementIds.Any())
                                {
                                    //只保留当前存在证据节点的证据要素ID
                                    evidElementIds = evidElementIds.Where(x => evidNodeIds.Contains(x)).ToList();
                                    foreach (var evidElementId in evidElementIds)
                                    {
                                        EvidenceFactRelationModel evidFactModel = new EvidenceFactRelationModel();
                                        evidFactModel.ElementType = "2";
                                        evidFactModel.EvidenceElementId = evidElementId;
                                        evidFactModel.FactElenemtId = idAttrValue;
                                        //通过分人员要素与证据要素关系数据获取对应的证据文件Id
                                        var personRel = base.dataContext.DataLayerOpt.EvidenceFactRelations.Where(x => x.EvidenceElementId == evidElementId).FirstOrDefault();
                                        evidFactModel.EvidenceFileId = personRel?.EvidenceFileId;
                                        evidFactModel.Xpath = personRel?.Xpath;
                                        base.dataContext.DataLayerOpt.EvidenceFactRelations.Add(evidFactModel);
                                    }
                                }
                            }
                        }
                        nodeItem.Attribute("initialnodeid").Remove();
                    }
                }
                resultContent = mergedDoc.ToString();
            }
            return resultContent;
        }
    }
}
